package com.mxpio.erp.quality.service.impl;

import com.mxpio.erp.common.enums.NoticeStatusEnums;
import com.mxpio.erp.common.inventory.entity.WarehouseOrderLine;
import com.mxpio.erp.common.jpa.model.Notice;
import com.mxpio.erp.common.quality.entity.SoInQualityOrder;
import com.mxpio.erp.common.quality.entity.SoInQualityOrderLine;
import com.mxpio.erp.common.quality.service.SoInQualityOrderService;
import com.mxpio.erp.common.sales.entity.SalesOrder;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.JpaUtil;
import com.mxpioframework.system.service.impl.BaseServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Service
public class SoInQualityOrderServiceImpl extends BaseServiceImpl<SoInQualityOrder> implements SoInQualityOrderService {
    @Override
    @Transactional(readOnly = true)
    public Result<List<SoInQualityOrder>> listByIds(String noticeNos) {
        if (StringUtils.isBlank(noticeNos)){
            return Result.error("查询参数为空");
        }
        String[] split = noticeNos.split(",");
        List<String> noticeNoList = Stream.of(split).collect(Collectors.toList());
        List<SoInQualityOrder> list = JpaUtil.linq(SoInQualityOrder.class)
                .collect("noticeNo", SoInQualityOrderLine.class, "noticeNo")
                .collect("bizNo", SalesOrder.class,"bizOrderNo")
                .in("noticeNo", noticeNoList).list();
        if (list.size()>0){
            //关联下游仓单行中对应的数量
            List<WarehouseOrderLine> wolines = JpaUtil.linq(WarehouseOrderLine.class).in("originNoticeNo", noticeNoList).list();
            if (wolines.size()>0){//关联生成的仓单明细数量，即为通知单明细对应的确认数量
                Map<String, List<WarehouseOrderLine>> wolinesMap = wolines.stream().collect(Collectors.groupingBy(WarehouseOrderLine::getOriginNoticeNo));
                for (SoInQualityOrder notice:list){
                    List<SoInQualityOrderLine> noticeLines = notice.getSoInQualityOrderLines();
                    String noticeNo = notice.getNoticeNo();
                    if (wolinesMap.containsKey(noticeNo)){
                        List<WarehouseOrderLine> wolinesPerNo = wolinesMap.get(noticeNo);
                        if (wolinesPerNo!=null&&wolinesPerNo.size()>0){
                            Map<String, WarehouseOrderLine> wolinesPerNoMap = JpaUtil.index(wolinesPerNo, "originNoticeLineNo");
                            for (SoInQualityOrderLine noticeLine:noticeLines){
                                String lineNo = noticeLine.getLineNo();
                                if (wolinesPerNoMap.containsKey(lineNo)&&wolinesPerNoMap.get(lineNo)!=null){
                                    WarehouseOrderLine warehouseOrderLine = wolinesPerNoMap.get(lineNo);
                                    noticeLine.setWoLineQuantity(warehouseOrderLine.getQuantity());//关联确认数量
                                }
                            }
                        }
                    }
                }
            }
        }
        return Result.OK(list);
    }

    @Override
    @Transactional
    public Result<? extends Notice> reject(String noticeNo) {//拒绝
        SoInQualityOrder notice = this.getById(SoInQualityOrder.class,noticeNo);
        notice.setNoticeStatus(NoticeStatusEnums.NoticeStatus.REJECT.getStatusCode());
        this.update(notice);
        return Result.OK(notice);
    }
}
